package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntPhbsjsybgBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntPhbsjsybgBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntPhbsjsybgBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntPhbsjsybgBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntPhbsjsybgBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 普通混凝土配合比设计试验检测报告子Service业务层处理
 *
 * @author zxs
 * @date 2024-08-28
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntPhbsjsybgBaDetailServiceImpl implements IReportSnhntPhbsjsybgBaDetailService {

    private final ReportSnhntPhbsjsybgBaDetailMapper baseMapper;

    /**
     * 查询普通混凝土配合比设计试验检测报告子
     */
    @Override
    public ReportSnhntPhbsjsybgBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询普通混凝土配合比设计试验检测报告子列表
     */
    @Override
    public TableDataInfo<ReportSnhntPhbsjsybgBaDetailVo> queryPageList(ReportSnhntPhbsjsybgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntPhbsjsybgBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntPhbsjsybgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询普通混凝土配合比设计试验检测报告子列表
     */
    @Override
    public List<ReportSnhntPhbsjsybgBaDetailVo> queryList(ReportSnhntPhbsjsybgBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntPhbsjsybgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntPhbsjsybgBaDetail> buildQueryWrapper(ReportSnhntPhbsjsybgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntPhbsjsybgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntPhbsjsybgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntPhbsjsybgBaDetail::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntPhbsjsybgBaDetail::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpggph()), ReportSnhntPhbsjsybgBaDetail::getYangpggph, bo.getYangpggph());
        lqw.eq(StringUtils.isNotBlank(bo.getShengccjcd()), ReportSnhntPhbsjsybgBaDetail::getShengccjcd, bo.getShengccjcd());
        lqw.eq(StringUtils.isNotBlank(bo.getJincrq()), ReportSnhntPhbsjsybgBaDetail::getJincrq, bo.getJincrq());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntPhbsjsybgBaDetail::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getMid()), ReportSnhntPhbsjsybgBaDetail::getMid, bo.getMid());
        return lqw;
    }

    /**
     * 新增普通混凝土配合比设计试验检测报告子
     */
    @Override
    public Boolean insertByBo(ReportSnhntPhbsjsybgBaDetailBo bo) {
        ReportSnhntPhbsjsybgBaDetail add = BeanUtil.toBean(bo, ReportSnhntPhbsjsybgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改普通混凝土配合比设计试验检测报告子
     */
    @Override
    public Boolean updateByBo(ReportSnhntPhbsjsybgBaDetailBo bo) {
        ReportSnhntPhbsjsybgBaDetail update = BeanUtil.toBean(bo, ReportSnhntPhbsjsybgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntPhbsjsybgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除普通混凝土配合比设计试验检测报告子
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
